<?php

namespace console\controllers;

use Yii;
use yii\db\Query;

class UserController extends BaseController
{
	
    public function actionNpuserimport()
    {
        $start = 1000000;
        $query = new Query();
        $last_id = $query->from('user')->where("id>{$start} and id<=1200000")->max('id');
        if ($last_id) {
            $last_id -= $start;
        } else {
            $last_id = 0;
        }
        $sql = "SELECT u.*,a1.name province_name,a2.name city_name,a3.name county_name FROM np_user as u 
            LEFT JOIN dict_area as a1 on a1.code=u.province
            LEFT JOIN dict_area as a2 on a2.code=u.city
            LEFT JOIN dict_area as a3 on a3.code=u.county
            where u.id>{$last_id} AND status=1";
        $rows = Yii::$app->db_np->createCommand($sql)->queryAll();
        if (!empty($rows)) {
        	$data = [];
            foreach ($rows as $k => $v) {
                $id = $start + $v['id'];
                if ($v['pid'] > 0) {
                    $pid = $start + $v['pid'];
                } else {
                    if ($v['town'] > 0) {
                        $parent_sql = "SELECT * FROM np_user WHERE town=0 AND county='{$v['county']}' AND status=1";
                        $parent = Yii::$app->db_np->createCommand($parent_sql)->queryOne();
                        if ($parent) {
                            $pid = $start + $parent['id'];
                        }
                    } else {
                        $pid = 0;
                    }
                }
                if ($v['town'] > 0) {
                    $level = 14;
                } elseif ($v['county'] > 0) {
                    $level = 13;
                } elseif ($v['city'] > 0) {
                    $level = 12;
                } else {
                    $level = 11;
                }
                $data[] = [$id, $pid, $level, $v['name'], $v['account'], $v['hosname'], $v['task'], $v['province'], $v['city'], $v['county'], $v['province_name'] . '-' . $v['city_name'] . '-' . $v['county_name'], $v['desc'], $v['utime'], $v['ctime']];
            }
            Yii::$app->db->createCommand()->batchInsert('user', [
                'id', 'pid', 'level', 'realname', 'mobile', 'unit', 'task_amount', 'province', 'city', 'county', 'pcc', 'remark', 'updated_at', 'created_at'
            ], $data)->execute();
        }
    }

    public function actionNpdoctorimport()
    {
        $start = 1200000;
        $query = new Query();
        $last_id = $query->from('user')->where("id>{$start} and id<=3000000")->max('id');
        if ($last_id) {
            $last_id -= $start;
        }
        $sql = "SELECT u.*,a1.name province_name,a2.name city_name,a3.name county_name FROM np_doctor as u 
            LEFT JOIN dict_area as a1 on a1.code=u.province
            LEFT JOIN dict_area as a2 on a2.code=u.city
            LEFT JOIN dict_area as a3 on a3.code=u.county
            where u.id>{$last_id} AND status=1";
        $rows = Yii::$app->db_np->createCommand($sql)->queryAll();
        if (!empty($rows)) {
            $data = [];
            foreach ($rows as $k => $v) {
                $id = $start + $v['id'];
                if ($v['pid'] > 0) {
                    $pid = $start + $v['pid'];
                } else {
                    if ($v['town'] > 0) {
                        $parent_sql = "SELECT * FROM np_user WHERE town=0 AND county='{$v['county']}' AND status=1";
                        $parent = Yii::$app->db_np->createCommand($parent_sql)->queryOne();
                        if ($parent) {
                            $pid = $start + $parent['id'];
                        }
                    } else {
                        $pid = 0;
                    }
                }
                if ($v['town'] > 0) {
                    $level = 14;
                } elseif ($v['county'] > 0) {
                    $level = 13;
                } elseif ($v['city'] > 0) {
                    $level = 12;
                } else {
                    $level = 11;
                }
                $data[] = [$id, $pid, $level, $v['name'], $v['account'], $v['hosname'], $v['task'], $v['province'], $v['city'], $v['county'], $v['pcc'], $v['desc'], $v['utime'], $v['ctime']];
            }
            Yii::$app->db->createCommand->batchInsert('user', [
                'id', 'pid', 'level', 'realname', 'mobile', 'unit', 'task_amount', 'province', 'city', 'county', 'pcc', 'remark', 'updated_at', 'created_at'
            ], $data)->execute();
        }
    }

    public function actionNpchildimport()
    {
        
    }

    public function actionAdminimport()
    {
        $start = 3000000;
        $query = new Query();
        $last_id = $query->from('user')->where("id>{$start} and id<=3200000")->max('id');
        if ($last_id) {
            $last_id -= $start;
        } else {
            $last_id = 852;
        }
        $sql = "SELECT * FROM admin where id>{$last_id} AND status=1";
        $rows = Yii::$app->db->createCommand($sql)->queryAll();
        if (!empty($rows)) {
            $data = [];
            foreach ($rows as $k => $v) {
                if ($v['parent_id'] == 0) {
                    continue;
                }
                $id = $start + $v['id'];
                if ($v['parent_id'] == 1) {
                    $pid = 0;
                } elseif ($v['parent_id'] > 1) {
                    $pid = $start + $v['parent_id'];
                }
                if ($v['type'] == 6) {
                    $level = 15;
                } elseif ($v['type'] == 5) {
                    $level = 14;
                } elseif ($v['type'] == 4) {
                    $level = 13;
                } elseif ($v['city'] == 3) {
                    $level = 12;
                } else {
                    $level = 11;
                }
                $data[] = [$id, $pid, $level, trim($v['realname']), trim($v['phone']), trim($v['unitname']), $v['task'], $v['province'], $v['city'], $v['county'], $v['province_name'] . '-' . $v['city_name'] . '-' . $v['county_name'], $v['desc'], strtotime($v['update_time']), strtotime($v['create_time']), (!empty($v['birth']) && $v['birth'] > '1920-01-01' ? strtotime($v['birth']) : 0), $v['sex']];
            }
            Yii::$app->db->createCommand()->batchInsert('user', [
                'id', 'pid', 'level', 'realname', 'mobile', 'unit', 'task_amount', 'province', 'city', 'county', 'pcc', 'remark', 'updated_at', 'created_at', 'birth', 'sex'
            ], $data)->execute();
        }
    }

    public function actionChildimport()
    {
        
    }
}